package com.gyx.lottery.service.activitystatus.operator;

import com.gyx.lottery.common.utils.JacksonUtil;
import com.gyx.lottery.dao.dataobject.ActivityUserDO;
import com.gyx.lottery.dao.mapper.ActivityUserMapper;
import com.gyx.lottery.service.dto.ConvertActivityStatusDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends  AbstractActivityOperator{
    Logger logger= LoggerFactory.getLogger(UserOperator.class);
    @Autowired
    ActivityUserMapper activityUserMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needCovert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        if(activityId == null || CollectionUtils.isEmpty(userIds)){
            return false;
        }
        List<ActivityUserDO>userDTOList=activityUserMapper.selectByIdAndUserId(activityId,userIds);
        for(ActivityUserDO activityUserDO:userDTOList){
            if(activityUserDO==null ||activityUserDO.getStatus().equalsIgnoreCase(
                    convertActivityStatusDTO.getTargetUserStatus().name())){

                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean covert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try{
            activityUserMapper.updateStatusByIds(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getUserIds(),
                    convertActivityStatusDTO.getTargetUserStatus().name());
            return true;
        }
        catch (Exception e){
            logger.error("covert UserOpertor:{}",
                    JacksonUtil.writeVauleAsString(convertActivityStatusDTO)
            ,e);
            return false;
        }
    }
}
